package com.port.port_app.controller;

import com.port.port_app.service.ExcelService;
import com.port.port_app.service.SerialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping("/api/ATCommands")
public class AtCommandController {

    @Autowired
    private SerialService serialService;

    @Autowired
    private ExcelService excelService;

    @GetMapping("/index")
    public String getIndex(Model model) {
        return "portInfo";
    }

    /**
     * 获取串口
     *
     * @param portName
     * @return
     */
    @PostMapping("/getPortName")
    public ModelAndView getPortName(@RequestParam("portName") String portName, Model model) {
        try {
            if (portName == null) {
                //return "error";
                return new ModelAndView(new RedirectView("error"));
            }
            String portNameResponse = serialService.openPort(portName);
            System.out.println(portNameResponse);

            model.addAttribute("portNameResponse", portNameResponse);
            return new ModelAndView("portInfo", "portNameResponse", portNameResponse);
        } catch (Exception e) {
            //return "error";
            return new ModelAndView(new RedirectView("error"));
        }
    }

    /**
     * 进入临时模式
     *
     * @param
     * @return
     */
    @PostMapping("/getTemporaryMode")
    public String getTemporaryMode(Model model) {
        try {
            String temporaryResponses = serialService.temporaryMode();
            System.out.println(temporaryResponses);
            model.addAttribute("temporaryResponses", temporaryResponses);
            return "portInfo";
        } catch (Exception e) {
            return "error";
        }
    }


    /**
     * 关闭串口
     *
     * @return
     */
    @PostMapping("/closePort")
    public String closePort( Model model) {
        try {
            String closeResponse = serialService.closePort();
            System.out.println(closeResponse);
            model.addAttribute("closeResponse", closeResponse);
            return "portInfo";
        } catch (Exception e) {
            return "error";
        }
    }

    /**
     * 发送其中一个指令："+++","AT+DIVSERVE", "AT+VER", "AT+ICCID", "AT+SN", "AT+IMEI"
     *
     * @param command
     * @return
     */
    @PostMapping("/getAllCommandOfOnce")
    public String getAllCommandOfOnce(@RequestParam("command") String command, Model model) {
        try {
            if (command == null) {
                return "error";
            }
            String oneResponse = serialService.writeToPort(command);
            System.out.println("指令a--" + "--：" + command + "---接收数据=" + oneResponse);
            model.addAttribute("oneResponse", oneResponse);
            return "portInfo";
        } catch (Exception e) {
            return "error";
        }
    }

    /**
     * 一键写入指令
     * 获取消息后，写入表格
     *
     * @param model
     * @return
     */
    @PostMapping("/sendRecord")
    public String sendAtCommandsAndRecordToExcel(Model model) {
        try {
            // AT指令集
            String[] commands = {"AT+ICCID", "AT+SN", "AT+IMEI"};
            String[] allResponse = new String[commands.length];
            // 发送AT指令并接收响应
            for (int i = 0; i < commands.length; i++) {
                Thread.sleep(1000); // 稍等待响应
                allResponse[i] = serialService.writeToPort(commands[i]);
                System.out.println("指令-" + i + "-：" + commands[i] + "=" + allResponse[i]);
            }
            // 将响应写入Excel
            excelService.writeToExcel(allResponse);
            model.addAttribute("allResponse", allResponse);
            return "portInfo"; // 返回包含串口信息和AT指令列表的页面视图
        } catch (Exception e) {
            model.addAttribute("error", "Error occurred: " + e.getMessage());
            return "error";
        }
    }

}

